package com.xmb.wechat.delegate;

import android.content.Context;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import com.blankj.utilcode.util.ScreenUtils;
import com.blankj.utilcode.util.SizeUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.squareup.picasso.Picasso;
import com.xmb.wechat.R;
import com.xmb.wechat.baserecyclerview.BaseRecyclerViewManager;
import com.xmb.wechat.bean.PYQPicBean;
import com.xmb.wechat.definterface.PYQPicDelegateListener;
import com.xmb.wechat.picasso.PicassoPYQPicTransform;

import java.util.ArrayList;

/**
 * @Author lijiangop
 * @CreateTime 2020/5/9 17:42
 */
public class PYQAddPicDelegate extends BaseRecyclerViewManager {

    private PYQPicDelegateListener listener;
    private int                    mItemWidth;

    public PYQAddPicDelegate(Context context, RecyclerView recyclerView, PYQPicDelegateListener listener) {
        super(context, recyclerView);
        this.listener = listener;
        mItemWidth = (ScreenUtils.getScreenWidth() - SizeUtils.dp2px(30) - SizeUtils.dp2px(10)) / 3;//宽-recyclerView的padding-itemDecoration
    }

    @Override
    public BaseQuickAdapter initAdapter() {
        return new BaseQuickAdapter<PYQPicBean, BaseViewHolder>(R.layout.item_pyq_publish_pic, new ArrayList()) {
            @Override
            protected void convert(BaseViewHolder helper, PYQPicBean item) {
                if (TextUtils.isEmpty(item.getPic())) {
                    helper.setVisible(R.id.iv_add, true);
                    helper.setVisible(R.id.iv_pic, false);
                    helper.getView(R.id.iv_add).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (listener != null)
                                listener.onAdd();
                        }
                    });
                } else {
                    helper.setVisible(R.id.iv_add, false);
                    helper.setVisible(R.id.iv_pic, true);
                    ImageView imageView = helper.getView(R.id.iv_pic);
                    Picasso.get().load("file://" + item.getPic()).error(R.drawable.default_image)
                            .transform(new PicassoPYQPicTransform(mItemWidth))
                            .into(imageView);
                    helper.getView(R.id.iv_pic).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (listener != null)
                                listener.onOpen(item.getPic());
                        }
                    });
                    helper.getView(R.id.iv_pic).setOnLongClickListener(v -> {
                        new AlertDialog.Builder(context).setItems(new String[]{"删除"}, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                removeItem(mData.indexOf(item));
                                if (mData.size() < 9 && !TextUtils.isEmpty(mData.get(mData.size() - 1).getPic()))
                                    mData.add(new PYQPicBean());
                            }
                        }).setNegativeButton("取消", null).show();
                        return true;
                    });
                }
            }
        };
    }

    @Override
    protected RecyclerView.LayoutManager getLayoutManager() {
        return new GridLayoutManager(context, 3);
    }

    @Override
    protected RecyclerView.ItemDecoration getItenDecoration() {
        return null;
    }
}
